//
//  PersionInfoViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/25.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "PersionInfoViewController.h"
#import "BRStringPickerView.h"
#import "TZImagePickerController.h"

#import "TTOSSManager.h"

@interface PersionInfoViewController ()<TZImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *userIcon;//用户头像
@property (weak, nonatomic) IBOutlet UITextField *userNickName;//用户昵称


@property (nonatomic, copy) NSString *selectedIconUrl;

@property (nonatomic, strong) NSMutableArray *arrData;
@end

@implementation PersionInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}


#pragma mark 请求上传给服务器
- (IBAction)saveBtnClick:(id)sender {
    
    if (self.userNickName.text.length) {
        if (self.arrData.count) {
            [self uploadImages:self.arrData];
        } else {
            [self request_UpdateUserInfo];
        }
    } else {
        [self alert:@"请输入昵称"];
    }
}


- (void)initView {
    
    self.title = @"用户信息";

    self.userNickName.text = [TTUserModel sharedObject].userName;
    self.selectedIconUrl = [TTUserModel sharedObject].userIconURL;
    [self.userIcon sd_setImageWithURL:[NSURL URLWithString:[TTUserModel sharedObject].userIconURL] placeholderImage:[UIImage imageNamed:@"placeholder_rect"] options:SDWebImageRetryFailed];

    self.userIcon.userInteractionEnabled = YES;
    
    [self.userIcon addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
       
        TZImagePickerController *vc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
        
        vc.naviBgColor = [UIColor blackColor];
        vc.naviTitleColor = [UIColor whiteColor];
        vc.barItemTextColor = [UIColor whiteColor];
        
        vc.preferredLanguage = @"zh-Hans";
        
        //筛选到只剩图片
        vc.allowPickingVideo = NO;
        vc.allowPickingMultipleVideo = NO;
        vc.allowPickingGif = NO;
        vc.allowPickingVideo = NO;

//        vc.showSelectedIndex = YES;
//        vc.showSelectBtn = YES;
        
        //裁剪
        vc.allowCrop = YES;
        vc.cropRect = CGRectMake( 0 ,(TTScreenHeight - TTScreenWidth)/2, TTScreenWidth, TTScreenWidth);

        [vc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            
            [self.userIcon setImage:photos.firstObject];
            
            for (id img in photos) {
                if ([img isKindOfClass:[UIImage class]]) {
                    NSData *data = UIImageJPEGRepresentation(img, 1.0);
                    [self.arrData addObject:data];
                }
            }
            
        }];

        vc.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:vc animated:YES completion:nil];
        
    }];
    
}


- (void)uploadImages:(NSMutableArray *)array {
    
    [SVProgressHUD showWithStatus:@"正在往OSS图片服务器上传图片"];
    [TTOSSManager uploadPhoto:array type:@"avatar" FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD showWithStatus:@"已经获取到图片OSS地址"];
        [SVProgressHUD dismissWithDelay:1];
        if ([data isKindOfClass:[NSArray class]]) {
            
            self.selectedIconUrl = [NSString stringWithFormat:@"%@%@",OSS_IMAGE_PREFIX,[data firstObject]];
        }
        
        [self request_UpdateUserInfo];
        
    } UploadProgress:^(NSInteger index) {
//        NSLog(@"%ld",index);
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        [self alert:@"OSS图片服务器上传失败，请联系客服"];
    }];
    
}

- (void)request_UpdateUserInfo {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.selectedIconUrl forKey:@"avatar"];
    [dic setValue:self.userNickName.text forKey:@"username"];
    
    [SVProgressHUD showWithStatus:@"正在请求用户信息接口"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MINE_INFO_UPDATE];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                [self.navigationController popViewControllerAnimated:YES];
                [self alert:[data valueForKey:@"msg"]];

            } else {
                
                [self alert:[data valueForKey:@"msg"]];
            }
            
        } else {
            
        }
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
    
}

- (NSMutableArray *)arrData {
    if (!_arrData) {
        _arrData = @[].mutableCopy;
    }
    return _arrData;
}

@end
